package com.ckd.flyingtrip.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.MeBatteryRecordAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.javabean.MeBatteryRecordBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MeBatteryRecordActivity extends BaseActivity {

    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.activity_me_battery_record_recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.activity_me_battery_record_smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.activity_me_battery_record_no)
    TextView no;

    private List<MeBatteryRecordBean.DataBean.ListBean> listBeans = new ArrayList<>();
    private MeBatteryRecordAdapter adapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_battery_record);
        ButterKnife.bind(this);

        initView();
        initData();
        initListener();

    }

    private void initListener() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                smartRefreshLayout.setEnableLoadMore(true);
                page = 1;
                listBeans.clear();
                initData();
            }
        });
    }

    private void initView() {
        switch (getIntent().getStringExtra("type")) {
            case "1":
                title.setText("换电记录");
                break;
            case "4":
                title.setText("租车记录");
                break;
            case "5":
                title.setText("充电记录");
                break;
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new MeBatteryRecordAdapter(this, listBeans, getIntent().getStringExtra("type"));
        recyclerView.setAdapter(adapter);
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.POST, Constants.allsortsofrecord, new Response.Listener<String>() {

            @Override
            public void onResponse(String s) {
                Log.e("记录", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        MeBatteryRecordBean bean = new Gson().fromJson(s, MeBatteryRecordBean.class);
                        smartRefreshLayout.setEnableLoadMore(bean.getData().isHasNextPage());
                        listBeans.addAll(bean.getData().getList());
                        if (adapter != null)
                            adapter.notifyDataSetChanged();
                        if (listBeans.size() == 0) {
                            recyclerView.setVisibility(View.GONE);
                            no.setVisibility(View.VISIBLE);
                        } else {
                            recyclerView.setVisibility(View.VISIBLE);
                            no.setVisibility(View.GONE);
                        }
                    } else {
                        Tools.toast(msg);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if (smartRefreshLayout != null) {
                    smartRefreshLayout.finishRefresh();
                    smartRefreshLayout.finishLoadMore();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("page", page + "");
                    json_url.put("row", "10");
                    json_url.put("type", getIntent().getStringExtra("type"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    @OnClick(R.id.toolber_color_back)
    public void onViewClicked() {
        finish();
    }
}
